aboutsummaryrefslogtreecommitdiffstats
path: root/src/routes/(app)/[username]/invoice/[hash]/+layout.server.js
blob: b5323d3f47cb04877c8659d20d10c003ec8a672f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import Qr from 'qrcode-base64';
import { get } from '$lib/utils';
import { redirect } from '@sveltejs/kit';

export async function load({ depends, params, url, parent }) {
	depends('app:invoice');

	let { user } = await parent();
	let { hash } = params;
	let invoice = await get(`/invoice/${hash}`);

	if (user && invoice.uid !== user.id && !url.pathname.includes('tip'))
		throw redirect(307, `/send/invoice/${hash}`);

	let { amount, received } = invoice;
	amount = parseInt(amount);

	let paid = (!amount && received) || (amount > 0 && received >= amount);
	if (paid && !url.pathname.endsWith('paid'))
		throw redirect(307, `/${params.username}/invoice/${hash}/paid`);

	let sm = Qr.drawImg(invoice.text, { size: 300 });

	return { hash, invoice, sm, lg: sm };
}